package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.service.OwnPackagingWorkOrderService;
import org.example.mrrope.utils.CusutUtil;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.FormUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 包装 个人派工单
 */
@Service
@Slf4j
public class OwnPackagingWorkOrderServiceImpl implements OwnPackagingWorkOrderService {
    @Autowired
    private DingUtil dingUtil;

    @Override
    @Async
    public Integer saveOwnPackagingWorkOrder(String id) {
        try {
            //得到包装车间派工单数据
            log.info("开始调用宜搭查询接口-----------------");
            log.info(id);
            JSONObject jsonObject = new JSONObject(dingUtil.getYidaById(id));
            log.info("调用查询接口返回包装车间派工单数据" + jsonObject);
            // body
            int res = 1;
            if (jsonObject.has("tableField_lx1lyoqe")) {
                JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lx1lyoqe");
                for (int i = 0; i < OwnerTable.length(); i++) {
                    JSONObject ownJson = getUpJson(jsonObject,id);
                    ownJson.put("employeeField_lx3zlria", OwnerTable.getJSONObject(i).get("employeeField_lx1lyoqf_id"));
                    ownJson.put("numberField_lx3zlrib", OwnerTable.getJSONObject(i).optInt("numberField_lx1lyoqg"));
                    ownJson.put("numberField_lz9lw73s", OwnerTable.getJSONObject(i).get("numberField_lx1lyoqg"));
                    ownJson.put("numberField_lz9lw73r", OwnerTable.getJSONObject(i).get("numberField_lx1lyoqg"));
                    ownJson.put("textField_lz9kwvlu", CusutUtil.getRandomString(11));//uuid
                    log.info("*************新建包装车间个人派工单请求参数---->>>>" + ownJson);
                    //郭扬晨
                    String result = dingUtil.startInstance(ownJson, "314764473436461353", "FORM-5F84A1B863AF400D971C23183B59A6D4TI4O");
                    log.info("*************新建包装车间个人派工单返回参数---->>>>" + result);
                    if (result != null) {
                        log.info("新建包装车间个人派工单成功，单号====》" + result);
                        res = 0;
                    } else {
                        log.info("新建包装车间个人派工单失败");
                        res = 1;
                    }
                }
                return res;
            } else {
                log.info("未找到工人");
                return 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static JSONObject getUpJson(JSONObject jsonObject, String id) {
        JSONObject ownJson = new JSONObject();
        JSONArray jsonArrayOne = FormUtils.getAssociationFormField("FORM-710F168856364416B7E566E252E82DCD2TYH", jsonObject.get("textField_luga5zz1").toString(), id);
        ownJson.put("associationFormField_lxtux74w", jsonArrayOne);
        ownJson.put("dateField_luga5zyz", System.currentTimeMillis());//日期
        ownJson.put("textField_lwt4miut", jsonObject.get("textField_lwt4miut"));//客户名称
        ownJson.put("textField_luga5zz1", jsonObject.get("textField_luga5zz1"));//SO号
        ownJson.put("textField_lwt597ig", jsonObject.get("textField_lwt597ig"));//AC号
        ownJson.put("textField_lzca9rf7", jsonObject.get("textField_lzcc6a1q"));//产品名称
        if (jsonObject.has("imageField_lwt597ic")) {
            ownJson.put("imageField_lwt597ic", jsonObject.get("imageField_lwt597ic"));//成品图片
        }
        if (jsonObject.has("tableField_lwed3a1x")){
            JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lwed3a1x");
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < OwnerTable.length(); i++) {
                JSONObject ownJsonA = OwnerTable.getJSONObject(i);
                JSONObject ownJsonB = new JSONObject();
                ownJsonB.put("textField_lwt597ib", ownJsonA.get("textField_lujgt592"));//半成品货号
                ownJsonB.put("textField_lwt597id", ownJsonA.get("textField_lujgt57g"));//品名规格
                jsonObjects.add(ownJsonB);
            }
            ownJson.put("tableField_lwt597ia", jsonObjects);//半成品详情子表
        }
        if (jsonObject.has("tableField_lxwy02ud")){
            JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lxwy02ud");
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < OwnerTable.length(); i++) {
                JSONObject ownJsonA = OwnerTable.getJSONObject(i);
                JSONObject ownJsonB = new JSONObject();
                ownJsonB.put("textField_lxvknj79", ownJsonA.get("textField_lxwy02u6"));//包材编号
                ownJsonB.put("textField_lxvknj7a", ownJsonA.get("textField_lxwy02u7"));//规格描述
                ownJsonB.put("textField_lxvknj78", ownJsonA.get("textField_lxwy02u5"));//名称
                ownJsonB.put("imageField_lxvknj7e", ownJsonA.has("imageField_lxwy02u9")?ownJsonA.get("imageField_lxwy02u9"):"");//例图
                ownJsonB.put("textField_lxvknj7h", ownJsonA.get("textField_lxwy02ua"));//例图
                jsonObjects.add(ownJsonB);
            }
            ownJson.put("tableField_lxvknj7i", jsonObjects);//包材子表
        }
//        TODO 子表未生效
//        ownJson.put("tableField_lwt597ia", jsonObject.get("tableField_lwed3a1x"));//半成品详情子表
//        ownJson.put("tableField_lxvknj7i", jsonObject.get("tableField_lxwy02ud"));//包材子表
        ownJson.put("numberField_lujhx4e3", jsonObject.optInt("numberField_lujhx4e3"));//内袋/盒装量
        ownJson.put("numberField_lx3zlric", jsonObject.optInt("numberField_lx426odz"));//内袋/盒数量
        ownJson.put("numberField_lwt597ih", jsonObject.optInt("numberField_lwt597ih"));//总装量
        ownJson.put("textareaField_lvxftd6b", jsonObject.get("textareaField_lvxftd6b"));//包装方式
        ownJson.put("textareaField_lw61w5oe", jsonObject.get("textareaField_lw61w5oe"));//备注
        if (jsonObject.has("imageField_lujgt58p"))
            ownJson.put("imageField_lujgt58p", jsonObject.get("imageField_lujgt58p"));//唛头图片
        if (jsonObject.has("imageField_lujgt58q"))
            ownJson.put("imageField_lujgt58q", jsonObject.get("imageField_lujgt58q"));//吊牌正反图片
        if (jsonObject.has("imageField_lujgt58r"))
            ownJson.put("imageField_lujgt58r", jsonObject.get("imageField_lujgt58r"));//吊牌悬挂位置
        if (jsonObject.has("imageField_lvxhxh0b"))
            ownJson.put("imageField_lvxhxh0b", jsonObject.get("imageField_lvxhxh0b"));//吊牌贴纸图片
        if (jsonObject.has("imageField_lujgt58s"))
            ownJson.put("imageField_lujgt58s", jsonObject.get("imageField_lujgt58s"));//内袋贴纸图片
        ownJson.put("textField_lwed3a24", "包装车间");//客户名称
        return ownJson;
    }
}
